﻿using System.Linq;
using System.Windows;
using System.Threading;
using System.Windows.Data;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using FU.Capstones.IMS.Web.Silverlight.Popups;
using Microsoft.Practices.Prism.Commands;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class PartnerListViewModel: ViewModelBase
    {
        public PartnerListViewModel()
        {
            Model = new PartnerListModel();
            ((PartnerListModel)Model).DeleteCustomerCompleted += DoDeleteCustomerCompleted;
            ((PartnerListModel)Model).GetPartnerCollectionCompleted += DoGetPartnerCollectionCompleted;
            ((PartnerListModel)Model).GetSingleCustomerCompleted += DoGetSingleCustomerCompleted;
            ((PartnerListModel)Model).UndoDeletePartnerCompleted += UndoDeletePartnerCompleted;
            ((PartnerListModel)Model).SearhPartnerCompleted += DoSearchPartnerCompleted;

            PartnerCriteria = new Partner();

            ServiceCallCounter = 0;
            ServiceCallCounter += ((PartnerListModel)Model).GetPartnerCollectionAsync(IsShowAll);

            Message = WebConstants.LoadingMessage;

            CanEditOrAdd = CurrentUser.CanEditPartner;
            CanAdd = CurrentUser.CanAddPartner;
        }

        #region Do Completed Methods

        void UndoDeletePartnerCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
            }
            else
            {
                Message = e.Result.Message;
                if (e.Result.IsSuccessful)
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.PartnerDetail, currentPartnerID);
                }
            }

            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoGetPartnerCollectionCompleted(object sender, CollectionFeedbackCompletedEventArgs<Partner> e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                MessageVisibility = Visibility.Visible;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    MessageVisibility = Visibility.Visible;
                }
                else
                {
                    MessageVisibility = Visibility.Collapsed;
                    PartnerCollection = new PagedCollectionView(e.Result.Data);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoGetSingleCustomerCompleted(object sender, SingleFeedbackCompletedEventArgs<Partner> e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                MessageVisibility = Visibility.Visible;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    MessageVisibility = Visibility.Visible;
                }
                else
                {
                    MessageVisibility = Visibility.Collapsed;
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoDeleteCustomerCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
            }
            else
            {
                Message = e.Result.Message;
                if (e.Result.IsSuccessful)
                {
                    ServiceCallCounter += ((PartnerListModel)Model).GetPartnerCollectionAsync(IsShowAll);
                }
            }
            Thread.Sleep(3000);
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoSearchPartnerCompleted(object sender, CollectionFeedbackCompletedEventArgs<Partner> e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    PartnerCollection = new PagedCollectionView(e.Result.Data);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

       #endregion

        #region Commands

        #region ViewCommand

        private DelegateCommand<Partner> viewCommand;
        public DelegateCommand<Partner> ViewCommand
        {
            get
            {
                if (viewCommand == null)
                {
                    viewCommand = new DelegateCommand<Partner>(DoViewCommand);
                }
                return viewCommand;
            } 
        }

        private void DoViewCommand(Partner obj)
        {
            ServiceCallCounter += ((PartnerListModel)Model).GetSingleCustomerAsync(obj.PartnerID);
        }

        #endregion

        #region ShowAllCommand

        private DelegateCommand<object> showAllCommand;
        public DelegateCommand<object> ShowAllCommand
        {
            get
            {
                if (showAllCommand == null)
                {
                    showAllCommand = new DelegateCommand<object>(DoShowAllCommand);
                }
                return showAllCommand;
            }
        }

        private void DoShowAllCommand(object obj)
        {
            ServiceCallCounter +=
                ((PartnerListModel)Model).GetPartnerCollectionAsync(IsShowAll);
        }

        #endregion

        #region EditCommand

        private DelegateCommand<int?> editCommand;
        public DelegateCommand<int?> EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new DelegateCommand<int?>(DoEditCommand);
                }
                return editCommand;
            }
        }

        private void DoEditCommand(int? obj)
        {
            if (obj.HasValue)
            {
                currentPartnerID = obj.Value;
                var temp = PartnerCollection.Cast<Partner>().FirstOrDefault(x => x.PartnerID == obj.Value);
                if (temp.IsDelete)
                {
                    var popup = new UndoDeleteConfirmationPopup();
                    popup.Show();
                    popup.Closed += (sender, e) =>
                                        {
                                            if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                                            {
                                                ServiceCallCounter += (Model as PartnerListModel).UndoDeletePartnerAsync(currentPartnerID);
                                            }
                                        };
                }
                else
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.PartnerDetail, obj.Value);
                }
            }
        }

        #endregion

        #region DeleteCommand

        private DelegateCommand<int?> deleteCommand;
        public DelegateCommand<int?> DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new DelegateCommand<int?>(DoDeleteCommand);
                }
                return deleteCommand;
            }
        }

        private void DoDeleteCommand(int? obj)
        {
            if (obj.HasValue)
            {
                currentPartnerID = obj.Value;
                DeleteConfirmationPopup popup = new DeleteConfirmationPopup();
                popup.Show();
                popup.Closed += (sender, e) =>
                                    {
                                        if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                                        {
                                            ServiceCallCounter += ((PartnerListModel)Model).DeleteCustomerAsync(currentPartnerID);
                                            Message = "Deleting the selected Customer!";
                                        }
                                    };
            }
        }
        #endregion

        #region CreateCommand

        private DelegateCommand<Partner> createCommand;
        public DelegateCommand<Partner> CreateCommand
        {
            get
            {
                if (createCommand == null)
                {
                    createCommand = new DelegateCommand<Partner>(DoCreateCommand);
                }
                return createCommand;
            }
        }

        private void DoCreateCommand(object obj)
        {
            NavigationManager.NavigateTo(WebConstants.NavigationPages.PartnerDetail);
        }

        #endregion

        #region SearchCommand

        private DelegateCommand<object> searchCommand;
        public DelegateCommand<object> SearchCommand
        {
            get
            {
                if (searchCommand == null)
                {
                    searchCommand = new DelegateCommand<object>(DoSearchCommand);
                }
                return searchCommand;
            }
        }

        private void DoSearchCommand(object obj)
        {
            PartnerCriteria.IsSupplier = null;
            
            if (PartnerCriteria.IsSupplierSearch != PartnerCriteria.IsCustomerSearch)
                PartnerCriteria.IsSupplier = PartnerCriteria.IsSupplierSearch;

            ServiceCallCounter += ((PartnerListModel)Model).SearchPartnerAsync(PartnerCriteria);
            Message = WebConstants.SearchingMessage;
        }

        #endregion

        #endregion

        #region Binding Properties

        private PagedCollectionView partnerCollection;
        public PagedCollectionView PartnerCollection
        {
            get { return partnerCollection; }
            set
            {
                if (!ReferenceEquals(partnerCollection, value))
                {
                    partnerCollection = value;
                    RaisePropertyChanged(() => PartnerCollection);
                }
            }
        }

        private Partner partnerCriteria;
        public Partner PartnerCriteria
        {
            get { return partnerCriteria; }
            set
            {
                if (!ReferenceEquals(partnerCriteria, value))
                {
                    partnerCriteria = value;
                    RaisePropertyChanged(() => PartnerCriteria);
                }
            }
        }

        #endregion

        #region Fields

        private int currentPartnerID = 0;

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {
            //((EmployeeListModel) model).GetEmployeeCollectionAsync();
        }

        public override void OnLeave()
        {
            //throw new NotImplementedException();
        }

        #endregion
    }
}
